package com.jiantong.work.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.impl.dao.VaccinePurchaseRecordDao;
import com.jiantong.work.model.QuarantineCenterHarmless;
import com.jiantong.work.model.ReportHandleResume;
import com.jiantong.work.model.VaccinePurchaseRecord;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by maowenjie on 2017/4/17.
 */
@Component
public class VaccinePurchaseRecordManager {
    private final VaccinePurchaseRecordDao vaccinePurchaseRecordDao;

    private final ReportHandleResumeDao reportHandleResumeDao;

    @Autowired
    public VaccinePurchaseRecordManager(VaccinePurchaseRecordDao vaccinePurchaseRecordDao, ReportHandleResumeDao reportHandleResumeDao) {
        this.vaccinePurchaseRecordDao = vaccinePurchaseRecordDao;
        this.reportHandleResumeDao = reportHandleResumeDao;
    }

    @Transactional
    public Map<String , Object> createVaccinePurchaseRecord(VaccinePurchaseRecord vaccinePurchaseRecord, Integer toStatus){
        Integer fromStatus = vaccinePurchaseRecord.getStatus();
        ReportHandleResume reportHandleResume;
        Map<String , Object> map = Maps.newHashMap();

        LoginUser loginUser = UserUtil.getCurrentUser();
        String prefix = "CYM";
        prefix = Utils.createPrefix(prefix,loginUser);//单据编号前缀的开头字母 和 当前用户
        String number = Utils.create(prefix,vaccinePurchaseRecordDao.findCurrentMaxNumber(prefix));//要在表中拿到当前最大的Number号
        vaccinePurchaseRecord.setNumber(number);
        vaccinePurchaseRecord.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());
        if(toStatus != null && !fromStatus.equals(toStatus)){
            vaccinePurchaseRecord.setStatus(toStatus);
            vaccinePurchaseRecordDao.create(vaccinePurchaseRecord);
            map.put("vaccinePurchaseId",vaccinePurchaseRecord.getId());

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(vaccinePurchaseRecord.getId());
            reportHandleResume.setReportNumber(vaccinePurchaseRecord.getNumber());
            reportHandleResume.setType(DocumentsType.VACCINE_PURCHASE_RECORD_MANAGER.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            vaccinePurchaseRecordDao.create(vaccinePurchaseRecord);
            map.put("vaccinePurchaseId",vaccinePurchaseRecord.getId());
        }
        return map;
    }

    @Transactional
    public Map<String , Object> updateVaccinePurchaseRecord(VaccinePurchaseRecord vaccinePurchaseRecord,Integer toStatus){
        Integer fromStatus = vaccinePurchaseRecord.getStatus();
        ReportHandleResume reportHandleResume;
        Boolean flagVaccinePurchaseRecord;
        Map<String , Object> map = Maps.newHashMap();
        if(toStatus != null && !fromStatus.equals(toStatus)){
            vaccinePurchaseRecord.setStatus(toStatus);
            flagVaccinePurchaseRecord = vaccinePurchaseRecordDao.update(vaccinePurchaseRecord);
            map.put("flagVaccinePurchase",flagVaccinePurchaseRecord);

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(vaccinePurchaseRecord.getId());
            reportHandleResume.setReportNumber(vaccinePurchaseRecord.getNumber());
            reportHandleResume.setType(DocumentsType.VACCINE_PURCHASE_RECORD_MANAGER.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
//            reportHandleResume.setUserId(UserUtil.getCurrentUser().getId());
//            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            flagVaccinePurchaseRecord = vaccinePurchaseRecordDao.update(vaccinePurchaseRecord);
            map.put("flagVaccinePurchase",flagVaccinePurchaseRecord);
        }
        return map;
    }
    @Transactional
    public Boolean update(VaccinePurchaseRecord vaccinePurchaseRecord) {
        return vaccinePurchaseRecordDao.update(vaccinePurchaseRecord);
    }

}
